<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="小区" prop="communityId">
        <community-select v-model="queryParams.communityId" />
      </el-form-item>
      <el-form-item label="区域名称" prop="region">
        <el-input v-model="queryParams.region" placeholder="请输入区域名称" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="regionList"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column
        label="区域名称"
        prop="region"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="小区地址"
        prop="smallAddress"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建人"
        prop="createBy"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建时间"
        prop="createTime"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="更新时间"
        prop="updateTime"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作"
        width="180"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <addEdit
      v-model="open"
      :title="title"
      :row="curRow"
      v-if="open"
      @submit="getList"
    />
  </div>
</template>

<script setup name="AreaManage">
import { ref, getCurrentInstance } from "vue";
import CommunitySelect from "@/components/communitySelect/index.vue";
import addEdit from "./addEdit.vue";
import { getRegionPage, removeRegion } from "@/api/realEstateManage";

const { proxy } = getCurrentInstance();
const regionList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const queryRef = ref();

const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  communityId: undefined,
  region: "",
});

const open = ref(false);
const title = ref("");
const curRow = ref({});

/** 查询区域列表 */
function getList() {
  loading.value = true;
  getRegionPage(queryParams.value).then(({ data }) => {
    regionList.value = data.records;
    total.value = data.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNo = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryRef.value.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  // TODO: 实现多选功能
}

/** 新增按钮操作 */
function handleAdd() {
  open.value = true;
  title.value = "添加区域";
  curRow.value = {};
}

/** 修改按钮操作 */
function handleUpdate(row) {
  open.value = true;
  title.value = "修改区域";
  curRow.value = row;
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm("是否确认删除该区域？")
    .then(async function () {
      await removeRegion(row.id);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>

<style scoped lang="scss"></style>
